#include "addpaper.h"
#include "ui_addpaper.h"
#include <QMessageBox>
#include "dialog.h"
#include "bigsave.h"
#include <vector>
#include "paper.h"
#include "student.h"
#include "teacher.h"
extern vector<paper*>pdata;
extern vector<student*>sdata;
extern vector<Teacher*>tdata;
addpaper::addpaper(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::addpaper)
{
    ui->setupUi(this);
}

addpaper::~addpaper()
{
    delete ui;
}

void addpaper::on_pushButton_clicked()
{
    this->close();
    Dialog *p=new(Dialog);
    p->show();
}

void addpaper::on_pushButton_2_clicked()
{
    QString str1=ui->lineEdit->text();
    QString str2=ui->lineEdit_2->text();
    QString str3=ui->lineEdit_3->text();
    QString str4=ui->lineEdit_4->text();
    QString str5=ui->lineEdit_5->text();
    QString str6=ui->lineEdit_6->text();
    QString str7=ui->lineEdit_7->text();
    QString str8=ui->lineEdit_8->text();
    QString str9=ui->lineEdit_9->text();
    QString str10=ui->textEdit->toPlainText();
    if(str1==""||str2==""||str3==""||str4==""||str5==""||str6==""||str7==""||str8==""||str9==""||str10==""){
        QMessageBox::information(this,"提示","请确保信息完整");
    }
    else{
        int r=0;
        for(unsigned int i=0;i<pdata.size();i++){
            if(pdata[i]->ifhave(str2)){
                r=1;
            }
        }
        if(r==1){
        QMessageBox::information(this,"提示","此项目编号已存在");
        }
        else{
            vector<QString>name;
            QStringList list=str10.split(",");
                for(int i=0;i<list.size();i++){
                    name.push_back(list[i]);}
                if(str5=="是"){
                    str5="1";
                }
                else{
                    str5="0";
                }
                if(str6=="是"){
                    str6="1";
                }
                else{
                    str6="0";
                }
                if(str7=="是"){
                    str7="1";
                }
                else{
                    str7="0";
                }
                if(str8=="是"){
                    str8="1";
                }
                else{
                    str8="0";
                }
                if(str9=="是"){
                    str9="1";
                }
                else{
                    str9="0";
                }
                for(unsigned int i=0;i<name.size();i++){
                for(unsigned int j=0;j<tdata.size();j++){
                    if(tdata[j]->findname()==name[i]){
                        tdata[j]->addpaper();
                        break;
                    }
                }
                for(unsigned int k=0;k<sdata.size();k++){
                    if(sdata[k]->findname()==name[i]){
                        sdata[k]->addpaper();
                    }
                }}
            pdata.push_back(new paper(name,str1,str2,str3,str4,str5.toInt(),str6.toInt(),str7.toInt(),str8.toInt(),str9.toInt()));
            bigsave();
            QMessageBox::information(this,"提示","保存成功");
        }
    }
}
